I have a text file that needs to be
CodePudding user response:
First, create the dictionary containing these headers. Then read each line in this text file using commands like readline([n]). If your characters are special characters such as commas or spaces. Put these values in the keys in the dictionary. Then you can create a data frame by converting the dictionary to csv file easily with the pandas library of python. You can read the documentation of Pandas.
CodePudding user response:
import re
with open('/Users/archive/combined_data_1.txt') as f:
for line in f:
result = re.search(r"^(\d ),(\d ),(\d{4}-\d{2}-\d{2})"/gm, line)
result2 = {
if re.search(r"(^\d ):", line) is not None:
movie_id = re.search(r"(^\d ):", line).group(1)
elif result:
customer_id = result.group(1)
rating = result.group(2)
date = result.group(3)
data_list = [customer_id, rating, date, movie_id] #data that you want. you can store it as csv file
df1 = pd.DataFrame(data_list)
df1.to_csv('combineddata1.csv')
else:
continue }
Im getting the following syntax error:
CodePudding user response:
import re
with open('text.txt') as f: #replace text.txt with your text file path
for line in f:
result = re.search(r"^(\d ),(\d ),(\d{4}-\d{2}-\d{2})"gm, line)
if re.search(r"(^\d ):", line) is not None:
movie_id = re.search(r"(^\d ):", line).group(1)
elif result:
costomer_id = result.group(1)
rating = result.group(2)
date = result.group(3)
data_list = [costomer_id, rating, date, movie_id] #data that you want. you can store it as csv file
# YOUR CODE
else:
continue