Python Forum
Saving data from each row into separate txt
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving data from each row into separate txt
#7
I would guess it means that you have posted different "csv" file than you are actually processing...

Your posted output was probably pandas dataframe output (with line numbers and fields unquoted and separated by whitespaces), while your actual file seems to be real csv file with comma separator and quoted fields:
Output:
"FIPS","text" 1001,"I screwed that last tweet upAnd im ready to get..." 1003,"Like, please, we're all dying to know how many..." ...
If you process such file with DeaD_Eye's script, you naturally would get strange file names (anything to first whitespace would be in file name) and shortened text (and Dead_Eye's script would not survive file with line consisting just of whitespaces, but that should not be problem in "well" formatted csv).

As it seems that you are working with real csv file, use csv module - something like
import csv

with open('file.csv') as csvfile:
   reader = csv.reader(csvfile)
   next(reader) # throw away first line
   for row in reader: # row is already list of splitted fields
       if len(row) == 2: # write out only lines that have exactly two fields - id and tweet?
           with open(row[0] + ".txt", "w") as outfile:
               outfile.write(row[1] + "\n") 
If your file can contain more than two fields and you want them all, concatenate row[1:] into your output file (or use csv.writer and write csv file). And if you want to write out even "empty" tweets, then add check for len(row) == 1 writing empty file...
Reply


Messages In This Thread
RE: Saving data from each row into separate txt - by zivoni - Jul-04-2017, 09:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving data into xlxs colomwise erdemath 0 1,458 Oct-02-2021, 09:01 AM
Last Post: erdemath
  Data saving structure JosefFilosopio 0 2,171 May-04-2019, 10:48 AM
Last Post: JosefFilosopio
  Problem with saving data and loading data to mysql kirito85 4 4,025 Feb-08-2019, 10:53 AM
Last Post: kirito85
  Saving data into .mat (Matlab Fiile) Tulo_Stone 0 2,442 Feb-28-2018, 12:57 AM
Last Post: Tulo_Stone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020